package socket.InetAddress;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @description: 使用URL读取网页内容
 * @author: Fesine
 * @createTime:2016/9/29 21:29
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/9/29 21:29
 */
public class TestGetFromURL {
    public static void main(String[] args) {
        try {
            //创建url实例
            URL url = new URL("http://www.baidu.com");
            //通过url的openStream获取url对象所表示的资源的字节输入流
            InputStream is = url.openStream();
            //将字节输入流转化为字符输入流
            InputStreamReader isr = new InputStreamReader(is,"utf-8");
            //为字符流创建缓冲
            BufferedReader bf = new BufferedReader(isr);
            String data = bf.readLine();//读取数据
            while (data != null) {
                System.out.println(data);//输出数据
                data = bf.readLine();//循环读取数据
            }
            if (bf != null) {
                bf.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (is != null) {
                is.close();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
